﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TERP.WaterClient.Common;
using System.Data;
using CommonUtils;

namespace TERP.WaterClient.DbServices
{
	public abstract class BaseService
	{
		protected ITSService.WSITS _dbService;
		protected string _strGwUserCode;
		protected string _strGwPassword;
		protected string _strPartnerCode;
		protected string _strLangCode;
		protected string _strSessionId;
		protected string _strUserCode;

		public BaseService()
		{
            _dbService = new TERP.WaterClient.ITSService.WSITS();
			_dbService.Timeout = 1234560000;
			try
			{
                _dbService.Url = CommonUtils.Registry.GetKey("HKEY_CURRENT_USER\\Software\\ITS\\ERP.ServiceMng", "MainUrl", "http://localhost:2101/TERP.WSITS/WSITS.asmx").ToString();//SystemConfig.Instance.WsUrl;
			}
			catch (Exception) 
			{ }
            _strGwUserCode = SystemConfig.Instance.GwUserCode;
            _strGwPassword = SystemConfig.Instance.GwPassword;
            _strPartnerCode = SystemConfig.Instance.PartnerCode;
            _strLangCode = SystemConfig.Instance.SystemLanguage;
            _strSessionId = SystemGlobal.Instance.SessionId;
            _strUserCode = SystemGlobal.Instance.Username;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="ds"></param>
		/// <returns></returns>
		protected ServiceException GenServiceException(DataSet ds){
			string errorCode = Convert.ToString(CMyDataSet.GetErrorCode(ds));
			string errorMessage = Convert.ToString(CMyDataSet.GetErrorCode(ds));
			string errorDetail = "No detail!";
			object[] arrObj = CMyDataSet.GetErrorParams(ds);
			if (arrObj != null && arrObj.Length > 1){
				errorDetail = "";
				for (int i=0;i<arrObj.Length;i++){
					errorDetail += Convert.ToString(arrObj[i]);
					if (i % 2 == 0) 
						errorDetail += " = ";
					else
						errorDetail += String.Format("{0}-----------------------------------------{1}",System.Environment.NewLine ,System.Environment.NewLine);
				}
			}
			ServiceException ex = new ServiceException();
			ex.ErrorCode = errorCode;
			ex.ErrorMessage = errorMessage;
			ex.ErrorDetail = errorDetail;
			return ex;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="arrTblNameCouple"></param>
		/// <param name="dsData"></param>
        public void SaveMasterDataTable(string[] arrTblNameCouple, DataSet dsData)
        {
            string strTId = SystemGlobal.Instance.GetNextTid();
            DataSet mdsFinal = null;
            //
            try
            {
                mdsFinal = _dbService.CommonSaveMasterData(
                    _strGwUserCode,
                    _strGwPassword,
                    strTId,
                    _strSessionId,
                    arrTblNameCouple,
                    dsData);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (CMyDataSet.HasError(mdsFinal))
            {
                throw GenServiceException(mdsFinal);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="strTId"></param>
        /// <param name="strSessionId"></param>
        /// <param name="strTableName"></param>
        /// <returns></returns>
        protected DataTable GetMasterDataTable(
            string strTableName)
        {
            string strTId = SystemGlobal.Instance.GetNextTid();
            DataSet mdsFinal = null;
            //
            try
            {
                mdsFinal = _dbService.CommonGetMasterData(
                    _strGwUserCode,
                    _strGwPassword,
                    strTId,
                    _strSessionId,
                    strTableName
                    , null);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            //
            if (CMyDataSet.HasError(mdsFinal))
            {
                throw GenServiceException(mdsFinal);
            }
            //
            return mdsFinal.Tables[strTableName];
        }

	}
}